/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rh.game.loader;

import com.rh.game.Character;

import com.rh.game.impl.GameObject1;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.TreeMap;
import java.util.Map;

/**
 *
 * @author Hayden
 */
public class WorldBuffer {
    private static void debug(Object o)  {
        // System.out.println("WorldBuffer: " + o);
        // System.out.println("@NPCS: " + npcBuffer.getNpcs());
    }
    
    public static enum Mode  {
        CONVERSATIONS, PLAYER, INITIALIZATION, MAPS, NPCS, OBJECTS, TILES;
    }
    
    private Mode mode = null;

    private GameEffectsBuffer initBuffer = new GameEffectsBuffer();
    public GameEffectsBuffer getInitialization()  {
        return initBuffer;
    }
    
    private GameObjectBuffer objectBuffer = new GameObjectBuffer();
    
    public List<GameObject1> getObjects()  {
        return objectBuffer.getObjects();
    }
    
    private CharacterBuffer playerBuffer = new CharacterBuffer();
    public List<Character> getPlayer()  {
        return playerBuffer.getNpcs();
    }
    
    private CharacterBuffer npcBuffer = new CharacterBuffer();
    public List<Character> getNpcs()  {
        return npcBuffer.getNpcs();
    }
    
    private ConversationBuffer conversationBuffer = new ConversationBuffer();
    public ConversationBuffer getConversationBuffer()  { return conversationBuffer; }
    
    private Tile1 tile = Tile1.NULL;
    private TreeMap<Byte, Tile1> tileMaps = new TreeMap<Byte, Tile1>();
    public Collection<Tile1> getTiles()  {
        return tileMaps.values();
    }

    private MapBuffer mapBuffer = new MapBuffer();
    public MapBuffer getMapBuffer()  { return mapBuffer; }
    
    private String startElement = null;
    
    void characters(String pValue)  {
        if (pValue == null)  pValue = "";
        pValue = pValue.trim();
        debug("Content *" + pValue + "*, " + mode + ", " + startElement);
        if (mode == Mode.TILES)  {
            tile = tile.update(startElement, pValue);
        }
        else if (mode == Mode.MAPS)  {
            mapBuffer.characters(pValue);
        }
        else if (mode == Mode.CONVERSATIONS)  {
            conversationBuffer.characters(pValue);
        }
        else if (mode == Mode.OBJECTS)  {
            objectBuffer.characters(pValue);
        }
        else if (mode == Mode.INITIALIZATION)  {
            initBuffer.characters(pValue);
        }
        else if (mode == Mode.NPCS)  {
            npcBuffer.characters(pValue);
        }
        else if (mode == Mode.PLAYER)  {
            playerBuffer.characters(pValue);
        }
    }
    
    void startElement(String pName)  {
        startElement = pName;
        debug("Start Element *" + pName + "*");
        
        if ("tiles".equals(pName))  {
            mode = Mode.TILES;
        }
        else if ("maps".equals(pName))  {
            mode = Mode.MAPS;
        }
        else if ("npcs".equals(pName))  {
            mode = Mode.NPCS;
        }
        else if ("player".equals(pName))  {
            mode = Mode.PLAYER;
        }
        else if ("objects".equals(pName))  {
            mode = Mode.OBJECTS;
        }
        else if ("init".equals(pName))  {
            mode = Mode.INITIALIZATION;
        }
        else if ("conversations".equals(pName))  {
            mode = Mode.CONVERSATIONS;
        }
        
        if (mode == Mode.TILES)  {
            if ("tile".equals(pName))  {
                tile = Tile1.NULL;
            }
            else if ("Walkable".equals(pName))  {
                tile = tile.update("Walkable", "TRUE");
            }
        }
        else if (mode == Mode.MAPS)  {
            mapBuffer.startElement(pName);
        }
        else if (mode == Mode.CONVERSATIONS)  {
            conversationBuffer.startElement(pName);
        }
        else if (mode == Mode.INITIALIZATION)  {
            initBuffer.startElement(pName);
        }
        else if (mode == Mode.OBJECTS)  {
            objectBuffer.startElement(pName);
        }
        else if (mode == Mode.NPCS)  {
            npcBuffer.startElement(pName);
        }
        else if (mode == Mode.PLAYER)  {
            playerBuffer.startElement(pName);
        }
    }
    
    void endElement(String pName)  {
        startElement = "";
        debug("End Element *" + pName + "*");
                
        if (mode == Mode.NPCS)  {
            npcBuffer.endElement(pName);
        }
        else if (mode == Mode.CONVERSATIONS)  {
            conversationBuffer.endElement(pName);
        }
        else if (mode == Mode.PLAYER)  {
            playerBuffer.endElement(pName);
        }
        else if (mode == Mode.OBJECTS)  {
            objectBuffer.endElement(pName);
        }
        else if (mode == Mode.INITIALIZATION)  {
            initBuffer.endElement(pName);
        }
        else if (mode == Mode.MAPS)  {
            mapBuffer.endElement(pName);
        }
        
        if ("tile".equals(pName))  {
            if (tile != Tile1.NULL)  {
                tileMaps.put(tile.getID(), tile);
                debug("Added Tile: " + tile);
            }
        }
        else if ("maps".equals(pName))  {
            mode = null;
        }
        else if ("init".equals(pName))  {
            mode = null;
        }
    }    
}
